
const Base = require("../base.js");
var Mdjs = require('md-js');
var axios = require('axios');
const PassThrough = require('stream').PassThrough;
/*chatGpt
 */
module.exports = class extends Base {
  async indexAction() {
    this.header("dxeersw", "44456")
    this.header("Content-Type", "text/event-stream")
    this.header("Cache-Control", "no-cache")
    this.header("Connection", "keep-alive")
    this.header("X-Accel-Buffering", "no")
    const steDa = new PassThrough()
    this.body = steDa
    const texte = this.get("text")
    const lishi = this.get("lishi")
    axios({
      method: 'get',
      url: 'http://47.89.243.134:8360/index/testchar',
      params: {
        text: texte,
        lishi
      },
      responseType: 'stream'
    })
      .then(response => {
        response.data.on('data', chunk => {
          steDa.write(`event: my_event\ndata: ${chunk.toString('utf8')}\n\n`)
          console.log(chunk.toString('utf8'));
        });
        response.data.on('end', chunk => {
          console.log("结束了");
          steDa.write(`event: stop\ndata: 没有了\n\n`)
          steDa.end()
          // stream.write("结束了");
        });
      })
      .catch(error => {
        console.log(error);
      });
  }

  async dxchargptAction() {
    this.header("dxeersw", "44456")
    this.header("Content-Type", "text/event-stream")
    this.header("Cache-Control", "no-cache")
    this.header("Connection", "keep-alive")
    this.header("X-Accel-Buffering", "no")
    const steDa = new PassThrough()
    this.body = steDa
    const texte = this.get("text")
    const gptRobotList = this.model("gptRobotList")

    const jhjser = JSON.parse(texte)
    if (this.get("srid")) {
      const res = await gptRobotList.field("SYSTEM").where({ id: this.get("srid") }).find()
      console.log(res.SYSTEM);
      console.log('88888');
      jhjser.unshift({
        role: "system",
        content: res.SYSTEM
      })
    }

    axios({
      method: 'post',
      url: 'http://47.254.71.54:8360/index',

      headers: {

      },
      data: { texte },
      responseType: 'stream'
    })
      .then(response => {
        response.data.on('data', chunk => {



          let jjnbse = ''
          // try {
          //   jjnbse = chunk.toString('utf8').replace('data:', "").trim()
          //   jjnbse = JSON.parse(jjnbse)
          //   if (jjnbse.choices[0].delta.content) {
          //     jjnbse = jjnbse.choices[0].delta.content
          //   } else {
          //     console.log(chunk.toString('utf8'));
          //   }
          // } catch (e) {
          //   jjnbse = ""
          // }
          // // console.log(jjnbse);
          // steDa.write(`event: my_event\ndata: ${jjnbse}\n\n`)
          try {
            jjnbse=chunk.toString('utf8').split(`"content":"`)[1].split(`"`)[0]
            jjnbse=jjnbse.replaceAll('\\n','<br>')
          } catch (e) {
            console.log(e);
          }
          steDa.write(`event: my_event\ndata: ${jjnbse}\n\n`)



        });
        response.data.on('end', chunk => {
          console.log("结束了");
          steDa.write(`event: stop\ndata: 没有了\n\n`)
          steDa.end()
          stream.write("结束了");
        });
      })
      .catch(error => {
        console.log(error);
      });
  }
}
